/*
 * @Description: 路由配置
 * @Author: Zeon scplay@gmail.com
 * @Date: 22017/8/2
 * @Copyright: Created by Panxsoft.
 *
 *  转场动画(has_transition)配置:  通过配置 meta 的 has_transition 属性来设置动画.
 */

// import errorReport from 'src/utils/error-report';
import VueRouter from 'vue-router';
import store from 'src/vuex/store';
import { messages, $t } from 'src/locales/index';
import { normalizeSearch, gTag } from 'src/libs/analytics';
import Toast from 'cube-ui/lib/toast';
import handleRouterBefore from './handleRouterBefore';
// 进入路由前的判断处理逻辑
const lang_type = store.state.lang.current_lang || 'zh-CN';
import { showLottieLoading, hideLottieLoading } from 'src/utils/global-loading'

/**
 * 关于路由的命名，如果父级路由有默认跳转的子路由，父级路由就不需要name命名了
 */
const routers = [
	{
		path: '/',
		meta: {
			title: messages[lang_type].router.user_info,
		},
		name: 'home',
		component(resolve) {
			return require.ensure([], () => resolve(require('./views/user-center/views')), 'user-views');
		},
		// component: resolve => require.ensure([], () => resolve(require('./views/user-center/views')), 'user-center'),
	},
	{
		path: '/user',
		name: 'user',
		meta: {
			title: messages[lang_type].router.user_info,
			has_transition: true,
		},

		component: resolve => require.ensure([], () => resolve(require('./views/user-center/home')), 'user-home'),

		children: [
			{
				path: '/',
				name: 'user-views',
				meta: {
					title: messages[lang_type].router.user_info,
					has_transition: true,
				},
				component: resolve => require.ensure([], () => resolve(require('./views/user-center/views')), 'user-views'),
			},
			{
				path: '/information',
				name: 'information',
				meta: {
					title: messages[lang_type].router.information,
					has_transition: true,
				},
				component: resolve => require.ensure([], () => resolve(require('./views/user-center/information/views')), 'information'),
			},
			{
				path: '/report',
				name: 'report',
				meta: {
					title: messages[lang_type].router.report,
					has_transition: true,
				},
				component: resolve => require.ensure([], () => resolve(require('./views/user-center/report/views')), 'report'),
			},
			{
				path: '/touch-skin-report',
				name: 'touch-skin-report',
				meta: {
					title: messages[lang_type].router.touch_skin_report,
					has_transition: true,
				},
				component: resolve => require.ensure([], () => resolve(require('./views/user-center/report/touch-skin-report')), 'touch-skin-report'),
			},
			{
				path: '/image-skin-report',
				name: 'image-skin-report',
				meta: {
					title: messages[lang_type].router.image_skin_report,
					has_transition: true,
				},
				component: resolve => require.ensure([], () => resolve(require('./views/user-center/report/image-skin-report')), 'image-skin-report'),
			},
			{
				path: '/collection',
				name: 'collection',
				meta: {
					title: messages[lang_type].router.collection,
					has_transition: true,
				},
				component: resolve => require.ensure([], () => resolve(require('./views/user-center/collection/views')), 'collection'),
			},
			{
				path: '/collection/:id',
				name: 'collection-detail',
				meta: {
					title: messages[lang_type].router.phone,
					has_transition: true,
				},
				component: resolve => require.ensure([], () => resolve(require('./views/user-center/collection/detail')), 'collection-detail'),
			},
			{
				path: '/recommend',
				name: 'recommend',
				meta: {
					title: $t('report.recommend_title'),
					has_transition: true,
				},
				component: resolve => require.ensure([], () => resolve(require('src/views/user-center/report/recommend.vue')), 'recommend'),
			},
			{
				path: '/card',
				name: 'card',
				meta: {
					title: messages[lang_type].router.card,
					has_transition: true,
				},
				component: resolve => require.ensure([], () => resolve(require('src/views/user-center/card/views')), 'card'),
			},
		],
	},
	{
		path: '/login',
		name: 'login',
		meta: {
			title: messages[lang_type].login.login,
		},
		component(resolve) {
			return require.ensure([], () => resolve(require('./views/login/views')), 'login');
		},
	},
	{
		path: '/lucky-money',
		name: 'lucky-money',
		meta: {
			title: '红包英雄榜',
		},
		component(resolve) {
			return require.ensure([], () => resolve(require('./views/lucky-money/views')), 'lucky-money');
		},
		children: [
			{
				path: '/',
				name: 'lucky-money-rankings',
				meta: {
					title: '红包英雄榜',
				},
				component: resolve => require.ensure([], () => resolve(require('./views/lucky-money/rankings/views')), 'lucky-money-rankings'),
			},
			{
				path: '/lucky-money-records',
				name: 'lucky-money-records',
				meta: {
					title: '红包记录', // 店员
				},
				component: resolve => require.ensure([], () => resolve(require('./views/lucky-money/record/staff')), 'lucky-money-staff-record'),
			},
			{
				path: '/lucky-money-bind-account',
				name: 'lucky-money-bind-account',
				meta: {
					title: '绑定支付宝',
					has_transition: true,
				},
				component: resolve => require.ensure([], () => resolve(require('./views/lucky-money/ali-account/bind')), 'lucky-money-bind-account'),
			},
			{
				path: '/lucky-money-edit-account',
				name: 'lucky-money-edit-account',
				meta: {
					title: '支付宝修改',
					has_transition: true,
				},
				component: resolve => require.ensure([], () => resolve(require('./views/lucky-money/ali-account/edit')), 'lucky-money-edit-account'),
			},
			{
				path: '/lucky-money-user-records',
				name: 'lucky-money-user-records',
				meta: {
					title: '红包记录', // 用户
					has_transition: true,
				},
				component: resolve => require.ensure([], () => resolve(require('./views/lucky-money/record/user')), 'lucky-money-user-records'),
			},
		],
	},
	{
		path: '/manual',
		meta: {
			title: messages[lang_type].router.manual,
			// has_transition: true,
		},
		component(resolve) {
			return require.ensure([], () => resolve(require('./views/manual/views')), 'manual');
		},
		children: [
			{
				path: '/',
				name: 'manual',
				meta: {
					title: messages[lang_type].router.manual,
				},
				component: resolve => require.ensure([], () => resolve(require('./views/manual/views')), 'manual'),
			},
			{
				path: 'ipad',
				name: 'ipad-manual',
				meta: {
					title: `${messages[lang_type].router.manual}-${messages[lang_type].router.ipad}`,
				},
				component: resolve => require.ensure([], () => resolve(require('./views/manual/ipad')), 'ipad-manual'),
			},
			{
				path: 'mirror',
				name: 'mirror-manual',
				meta: {
					title: `${messages[lang_type].router.manual}-${messages[lang_type].router.mirror}`,
				},
				component: resolve => require.ensure([], () => resolve(require('./views/manual/mirror')), 'mirror-manual'),
			},
		],
	},
	{
		path: '*',
		meta: {
			title: messages[lang_type].router.user_info,
		},
		component(resolve) {
			return require.ensure([], () => resolve(require('./views/user-center/views')), 'user-views');
		},
	},
	/**
     * 本地测试路由，上线时注销
     */
	require('./debug/debug-routes').default,
];

// 路由配置
const RouterConfig = {
	// mode: 'history',
	mode: 'hash',
	routes: routers,
};

const router = new VueRouter(RouterConfig);

// router.onError = function (error) {
// 	console.log('xx',error);
// };

/**
 * 路由前件
 * @param to
 * @param from
 * @param next
 */
function beforeEachRoute(to, from, next) {
	gTag('event', 'page_view', {
		page_path: `${window.location.pathname + normalizeSearch()}/${to.name}${window.location.hash}`,
	});

  showLottieLoading();

	// 路由进入前的处理逻辑
	handleRouterBefore(to, next);
}

/**
 * 路由后件处理
 */
function afterEachRoute() {
  hideLottieLoading();

	window.scrollTo(0, 0);
}

router.beforeEach(beforeEachRoute);
router.afterEach(afterEachRoute);

export default router;
